package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.odin.solution.contract.pc.GetMyMsgReq;
import com.homedo.odin.solution.contract.pc.GetMyMsgResp;
import com.homedo.odin.solution.contract.pc.InsertMsgReq;
import com.homedo.odin.solution.contract.pc.MsgTemplateResp;
import com.homedo.odin.solution.contract.pc.ReadMsgResp;
import com.homedo.odin.solution.contract.pc.UpdateMyMsgReq;
import com.homedo.odin.solution.service.bizservice.personal.MessageBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: donglibing
 * @date: 2018/3/6 14:39
 * @Description:
 */
@Api(description = "消息", tags = {"Message"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/personal")
public class MessageController extends BaseController<MessageBizService> {


    @ApiOperation(value = "根据uid获取消息列表", notes = "根据uid获取消息列表", response = GetMyMsgResp.class, httpMethod = "GET")
    @GetMapping("/message/list")
    public ResultData list(@ModelAttribute("req") GetMyMsgReq req) {

        GetMyMsgResp resp = baseService
                .getMessageList(req.getPageNum(), req.getPageSize(), req.getToUser(), req.getReaded());
        return ResultData.newInstance(Meta.CODE_200, resp);
    }

    @ApiOperation(value = "删除消息", notes = "删除消息")
    @DeleteMapping("/message/delete")
    public ResultData list(@ModelAttribute("req") UpdateMyMsgReq req) {

        baseService.deleteMessage(req.getIds(), req.getUserId());
        return ResultData.newInstance(Meta.CODE_200, "删除成功");
    }


    @ApiOperation(value = "标记未读消息为已读", notes = "标记未读消息为已读", httpMethod = "GET", response = ReadMsgResp.class)
    @GetMapping("/message/readmsg")
    public ResultData readMsg(@ModelAttribute("req") UpdateMyMsgReq req) {

        baseService.readMessage(req.getIds(), req.getUserId());
        return ResultData.newInstance(Meta.CODE_200, "标记为已读成功");
    }

    @ApiOperation(value = "插入消息", notes = "插入消息")
    @PostMapping("/message/insert")
    public ResultData insert(@RequestBody InsertMsgReq req) {

        baseService.insertMessage(req);
        return ResultData.newInstance(Meta.CODE_200, "插入信息成功");

    }

    @ApiOperation(value = "获取消息模板", notes = "获取消息模板", response = MsgTemplateResp.class, httpMethod = "GET")
    @GetMapping("/message/gettemplate/{code}")
    public ResultData getTemplate(@PathVariable("code") String code) {
        MsgTemplateResp resp = baseService.getMessageTemplate(code);
        return ResultData.newInstance(Meta.CODE_200, resp);
    }

}
